//index.js
//获取应用实例
const app = getApp()
var Util = require('../../utils/util.js');
Page({
  data: {
    // 首页轮播图
    imgUrls: [],
    indicatorDots: false,
    autoplay: false,
    interval: 3000,
    duration: 500,
    // 首页公告展示数据
    notice:{
      imgUrl: 'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640',
      url: ''
    }
  },
  toTabUrl:function(e){
    let url = e.currentTarget.dataset.tabUrl;
    let id = e.currentTarget.dataset.showId;
    wx.reLaunch({
      url: '../'+url+'?id='+id,
    })
  },
  toUrl: function (e){
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '../'+url,
    })
  },
  //事件处理函数
  toArticleInfo: function(e){
    let article_id = e.currentTarget.dataset.articleId;
    // console.log(article_id);
    wx.redirectTo({
      url: '/pages/index/index?id=' + article_id,
    })
  },
  //首页公告
  toNotice: function(e){
    let notice_url = e.currentTarget.dataset.notice;
    console.log("跳转到公告所指位置");
    wx.redirectTo({
      url: notice_url,
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    var data = {
      paraUrl: 'advertisement/list?position=frontPage&type=main',
      paraData: ''
    }
    app.getRequest(data, function (res) {
      if (res.data.result == 200) {
        that.setData({
          notice:{
            imgUrl: res.data.body[0].object.thumbnail ,
            url: ''
          } 
        });
      }
    })
    var data1 = {
      paraUrl: 'advertisement/list?position=frontPage&type=banner',
      paraData: ''
    }
    app.getRequest(data1, function (res) {
      if (res.data.result == 200) {
        that.setData({
          imgUrls: res.data.body
        });
      }
    })
    var data2 = {
      paraUrl: 'task/page?activity=true&keywords=&regionId&type&statusCode&page=1&size=4',
      paraData: ''
    }
    app.getRequest(data2, function (res) {
      if (res.data.result == 200) {
        that.setData({
          array_huodong: res.data.body.content
        });
      }
    })
  },
  toActivityDetail: function (e) {
    wx.setStorageSync("taskId", e.currentTarget.id)
    wx.navigateTo({
      url: '/pages/ActivityDetail/ActivityDetail'
    })
  },

	// 拍照功能
  getLocalImage: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        // 这里无论用户是从相册选择还是直接用相机拍摄，拍摄完成后的图片临时路径都会传递进来
        app.startOperating("保存中")
        var filePath = res.tempFilePaths[0];
        var session_key = wx.getStorageSync('session_key');
        // 这里顺道展示一下如何将上传上来的文件返回给后端，就是调用wx.uploadFile函数
        wx.uploadFile({
          url: app.globalData.url + '/home/upload/uploadFile/session_key/' + session_key,
          filePath: filePath,
          name: 'file',
          success: function (res) {
            app.stopOperating();
            // 下面的处理其实是跟我自己的业务逻辑有关
            var data = JSON.parse(res.data);
            if (parseInt(data.status) === 1) {
              app.showSuccess('文件保存成功');
            } else {
              app.showError("文件保存失败");
            }
          }
        })
      },
      fail: function (error) {
        console.error("调用本地相册文件时出错")
        console.warn(error)
      },
      complete: function () {

      }
    })
  }
})
